package com.franklin.ideaplugin.api.actions;

import com.franklin.ideaplugin.api.utils.find.MyFindUtil;
import com.intellij.find.EditorSearchSession;
import com.intellij.find.FindManager;
import com.intellij.find.FindModel;
import com.intellij.icons.AllIcons;
import com.intellij.icons.ExpUiIcons;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.ui.ExperimentalUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Ye Junhui
 * @since 2024/1/31
 */
public class MyFindAllAction extends AnAction implements ShortcutProvider, DumbAware {

    private final Project project;

    private static Icon getFindIcon() {
        return ExperimentalUI.isNewUI() ? ExpUiIcons.General.OpenInToolWindow : AllIcons.General.Pin_tab;
    }

    public MyFindAllAction(Project project) {
        super(IdeBundle.messagePointer(ExperimentalUI.isNewUI() ? "show.in.find.window.button.name.newui" : "show.in.find.window.button.name"),
                IdeBundle.messagePointer("show.in.find.window.button.description"), null);
        this.project = project;
    }

    @Override
    public void update(final @NotNull AnActionEvent e) {
        EditorSearchSession search = e.getData(EditorSearchSession.SESSION_KEY);
        Editor editor = search != null ? search.getEditor() : null;

        e.getPresentation().setIcon(getIcon(project));
        e.getPresentation().setEnabled(editor != null && project != null &&
                !project.isDisposed() && search.hasMatches() &&
                PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()) != null);
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }

    @Override
    public void actionPerformed(final @NotNull AnActionEvent e) {
        EditorSearchSession search = e.getRequiredData(EditorSearchSession.SESSION_KEY);
        if (project.isDisposed()) return;
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(search.getEditor().getDocument());
        if (file == null) return;

        FindModel oldModel = FindManager.getInstance(project).getFindInFileModel();
        FindModel newModel = oldModel.clone();
        String text = search.getTextInField();
        if (StringUtil.isEmpty(text)) return;

        newModel.setStringToFind(text);
        MyFindUtil.findAllAndShow(project, file, newModel);
    }

    @Override
    public @Nullable ShortcutSet getShortcut() {
        AnAction findUsages = ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_USAGES);
        return findUsages != null ? findUsages.getShortcutSet() : null;
    }

    private static @NotNull Icon getIcon(@Nullable Project project) {
        ToolWindowManager toolWindowManager = project != null ? ToolWindowManager.getInstance(project) : null;
        if (toolWindowManager != null) {
            return toolWindowManager.getLocationIcon(ToolWindowId.FIND, getFindIcon());
        }
        return getFindIcon();
    }
}
